﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WinApi.cs" company="melcoprog">
//   Copyright (c) melcoprog. All rights reserved.
// </copyright>
// <summary>
//   Defines the WinApi type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ESheep
{
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Text;

    using CreatureSheep;

    public static class WinApi
    {
        [DllImport("User32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);

        [DllImport("User32.dll")]
        private static extern void ReleaseDC(IntPtr dc);

        [DllImport("user32.dll")]
        private static extern IntPtr WindowFromPoint(Point Point);

        [DllImport("user32.dll")]
        private static extern bool GetWindowRect(IntPtr hwnd, ref Rect lpRect);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        public static IntPtr GetWindowFromPoint(int xPoint, int yPoint)
        {
            return WindowFromPoint(new Point(xPoint, yPoint));
        }

        public static bool GetWindowRectangle(IntPtr hwnd, ref Rect lpRect)
        {
            return GetWindowRect(hwnd, ref lpRect);
        }

        public static IntPtr WinApiGetDC(IntPtr hwnd)
        {
            return GetDC(hwnd);
        }

        public static void WinApiReleaseDC(IntPtr dc)
        {
            ReleaseDC(dc);
        }

        public static string GetWindowName(IntPtr hWnd)
        {
            var lpString = new StringBuilder(256);
            var result = GetWindowText(hWnd, lpString, 256);
            return lpString.ToString();
        }
    }
}
